package com.lucher.mes.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lucher.mes.common.Tools;
import com.lucher.mes.dao.WorkmachineMapper;
import com.lucher.mes.po.Workmachine;

@Controller
@RequestMapping("/workmachine")  
public class WorkmachineController extends BaseController{
	@Autowired
	private WorkmachineMapper workmachineDao;
	
	/**
	 * 查询信息
	 */
	@RequestMapping(value = "/findByList")
	public String findByList(Workmachine form) {
		return "workmachine/findWorkmachineList";
	}
	
	/**
	 * 查询信息Json
	 */
	@ResponseBody
	@RequestMapping(value = "/findListJson")
	public Map<String, Object> findListJson(Workmachine form) {
		//封装JSon的Map
		Map<String, Object> result = new HashMap<String, Object>(); 
		result.put("code", "0");
		List<HashMap<String, Object>> objectlist = new ArrayList<HashMap<String, Object>>();
		
		Integer total = workmachineDao.findByCount(form);
		List<Workmachine> list = workmachineDao.findByListForPage(form);
		
		for (Workmachine workmachine : list) {
			HashMap<String, Object> objectMap = new HashMap<String, Object>();
			
			objectMap.put("id", workmachine.getId());
			objectMap.put("code", workmachine.getCode());
			objectMap.put("model", workmachine.getModel());
			objectMap.put("addtime", StringUtils.isEmpty(workmachine.getAddtime())?"":Tools.getStr(workmachine.getAddtime()).substring(0, 19));
			objectMap.put("info", workmachine.getInfo());
			objectMap.put("state", workmachine.getState());
			objectMap.put("remark", workmachine.getRemark());
			objectlist.add(objectMap);
		}
		
		result.put("count", total);
		result.put("data", objectlist);
		return result;
	}

	/**
	 * 添加信息初始化
	 * 
	 * @return
	 */
	@RequestMapping(value = "/addInit")
	public String addInit(Workmachine form) {
		return "workmachine/addWorkmachine";
	}

	/**
	 * 新增
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value = "/save")
	public Map<String,Object>  save(Workmachine form) {
		
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		
		if ("".equals(form.getCode())) {
			responseJson.put("flag", "0"); //操作失败
			responseJson.put("msg", "操作失败，请输入编号");
			return responseJson;
		} else {
			Workmachine oldWorkmachine = workmachineDao.findByCode(form.getCode());
			if (oldWorkmachine != null) {
				responseJson.put("flag", "0");//操作失败
				responseJson.put("msg", "操作失败，此编号已经存在");
				return responseJson;
			}
		}
		
		//保存工位机信息
		form.setAddtime(Tools.getCurrentTime());
		Integer workmachineId = workmachineDao.insertSelective(form);
		
		responseJson.put("flag", "1");//操作成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}

	/**
	 * 更新初始化
	 */
	@RequestMapping(value = "/updateInit")
	public String updateInit(Workmachine form) {
		Workmachine workmachine = workmachineDao.selectByPrimaryKey(form.getId());
		form.setWorkmachine(workmachine);

		return "workmachine/updateWorkmachine";
	}

	/**
	 * 更新
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value = "/update")
	public Map<String,Object> update(Workmachine form) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		
		if ("".equals(form.getCode())) {
			responseJson.put("flag", "0"); //添加失败
			responseJson.put("msg", "操作失败，工位机编号不能为空");
			return responseJson;
		} else {
			Workmachine oldWorkmachine = workmachineDao.findByCode(form.getCode());
			if (oldWorkmachine != null && !oldWorkmachine.getId().equals(form.getId())) {
				responseJson.put("flag", "0"); //添加失败
				responseJson.put("msg", "操作失败，工位机编号已经存在");
				return responseJson;
			}
		}

		// 修改信息
		int flag = workmachineDao.updateByPrimaryKeySelective(form);
		
		responseJson.put("flag", "1");//
		responseJson.put("msg", "操作成功");
		return responseJson;
	}
    
	/**
	 * 删除
	 * @return
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value="/delete")
	public Map<String,Object> delete(Workmachine form) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		//删除工位机
		workmachineDao.deleteByPrimaryKey(form.getId());
		
		responseJson.put("flag", "1");//删除成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}
	
	/**
	 * 批量删除
	 * @return
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value="/deleteBatch")
	public Map<String,Object> deleteBatch(@RequestBody List<Workmachine> formlist) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		for (Workmachine workmachine : formlist) {
			//删除工位机
			workmachineDao.deleteByPrimaryKey(workmachine.getId());
		}

		responseJson.put("flag", "1");//删除成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}
	
}
